<template> 
  <div class="app-container" >
<editor-image></editor-image> 
<hello-world></hello-world>

    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        class="btn-add"
        @click="addAdmin()"
        size="mini">
        添加
      </el-button> 
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%" 
                v-loading="listLoading"
                border>
                <el-table-column type="selection" width="60" align="center"></el-table-column>
                      <el-table-column prop="brandId"
                                        label="brandId">
                          <template slot-scope="scope">
                              {{scope.row.id }}
                          </template>
                      </el-table-column>

                      <el-table-column prop="brandName"
                                        label="brandName"> 
                                        <template slot-scope="scope"> 
                                          <span style="margin-left: 10px">{{ scope.row.brandName }}</span>
                                        </template>
                      </el-table-column>
              <el-table-column label="是否禁用" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
  <!--          <el-button
                    size="mini"
                    @click="handleCommunity(scope.$index, scope.row)">绑定部门
            </el-button> -->
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[1,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {formatDate} from '@/utils/date';
  import {fetchList, update,deleteAdmin } from '@/api/pms/pmsProduct'
  import {getUserId} from '@/utils/auth';     

  import HelloWorld from '@/components/HelloWorld'

  export default {
    name: 'adminList',
    components: {
      HelloWorld
  },
    data() {
      return {
        operates: [
          {
            label: "显示会员",
            value: "showAdmin"
          },
          {
            label: "隐藏会员",
            value: "hideAdmin"
          }
        ],
        operateType: null,
        listQuery: {
          keyword: null,
          nameCn: null,
          pageNum: 1,
          pageSize: 10
        },
        list: null,
        total: null,
        listLoading: true,
        multipleSelection: []
      }
    },
    created() {
      
      this.getList();
      // console.log(getUserId())
    },
    filters:{
      formatTime(time) {
        if(time==null||time===''){
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatStatus(status){
        for(let i=0;i<defaultStatusOptions.length;i++){
          if(status===defaultStatusOptions[i].value){
            return defaultStatusOptions[i].label;
          }
        }
      },
      formatReturnAmount(row){
        return row.productRealPrice*row.productCount;
      }
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
          this.totalPage = response.data.pages;
          this.pageSize = response.data.size;
        });
      },
      // handleSelectionChange(val) {
      //   this.multipleSelection = val;
      // },
      // handleCommunity(index, row) {
      //   this.$router.push({path: '/sys/userCommunity', query: {id: row.id}})
      // },
      handleUpdate(index, row) {
        this.$router.push({path: '/pms/add', query: {id: row.id}})
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该商品', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAdmin(row.id).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      },
      // getProductList(index, row) {
      // },
      // getProductCommentList(index, row) {
      // },
      // handleFactoryStatusChange(index, row) {
      //   var data = new URLSearchParams();
      //   data.append("ids", row.id);
      //   data.append("factoryStatus", row.factoryStatus);
      //   updateFactoryStatus(data).then(response => {
      //     this.$message({
      //       message: '修改成功',
      //       type: 'success',
      //       duration: 1000
      //     });
      //   }).catch(error => {
      //     if (row.factoryStatus === 0) {
      //       row.factoryStatus = 1;
      //     } else {
      //       row.factoryStatus = 0;
      //     }
      //   });
      // },
      // handleShowStatusChange(index, row) {
      //   let data = new URLSearchParams();

      //   data.append("ids", row.id);
      //   data.append("showStatus", row.status);
      //   updateShowStatus(data).then(response => {
      //     this.$message({
      //       message: '修改成功',
      //       type: 'success',
      //       duration: 1000
      //     });
      //   }).catch(error => {
      //     if (row.status === 0) {
      //       row.status = 1;
      //     } else {
      //       row.status = 0;
      //     }
      //   });
      // },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      searchAdminList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      export(){

      },
      handleBatchOperate() {
        console.log(this.multipleSelection);
        if (this.multipleSelection < 1) {
          this.$message({
            message: '请选择一条记录',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let showStatus = 0;
        if (this.operateType === 'showAdmin') {
          showStatus = 1;
        } else if (this.operateType === 'hideAdmin') {
          showStatus = 0;
        } else {
          this.$message({
            message: '请选择批量操作类型',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        let data = new URLSearchParams();
        data.append("ids", ids);
        data.append("showStatus", showStatus);
        updateShowStatus(data).then(response => {
          this.getList();
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      addAdmin() {
        this.$router.push({path: '/sys/addAdmin'})
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>


</style>
